package com.bsoft.gol.editor.service.impl;


import com.alibaba.fastjson.JSONObject;

import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.request.InHospitalpatientListQueryRequest;
import com.bsoft.gol.editor.editor.response.InHospitalPatientResponse;
import hcn.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.bsoft.gol.editor.service.InHospitalPatientService;
import com.bsoft.gol.editor.utils.HttpClient;

/**
 * 住院患者his接口
 *
 * @author lxy
 * @date 2020/7/18
 */
@Component
public class InHospitalPatientServiceImpl implements InHospitalPatientService {


    private final static Logger log = LoggerFactory.getLogger(InHospitalPatientServiceImpl.class);


    /**
     * 获取住院病人列表-废弃
     *
     * @param request request
     * @return list
     */
    /*@Override
    public BaseResponse<List<InHospitalPatientResponse>> listPatient(InHospitalpatientListQueryRequest request) {
        log.info("listPatient：{}", request.toString());
        JSONObject jsonParams = (JSONObject) JSONObject.toJSON(request);
        //入参转换
        jsonParams.put("organizationId", jsonParams.remove("hospitalCode"));
        jsonParams.put("doctorId", jsonParams.remove("docJobNumber"));
        jsonParams.put("departmentId", jsonParams.remove("departmentCode"));
        JSONObject json = HttpClient.doMainPost(Constants.HIS_CLIENT_URL_01 + Constants.LIST_PATIENT, jsonParams);
        log.info("listPatient 第三方返回数据：{}", json);
        BaseResponse<List<InHospitalPatientResponse>> baseResponse = new BaseResponse<>();
        if (null != json && json.getInteger("code") == Constants.SUCCESS_CODE) {
            if (json.containsKey("data") && json.get("data") != null && !json.getJSONArray("data").isEmpty()) {
                List<InHospitalPatientResponse> inHospitalPatientResponseList = new ArrayList<>();
                List<InHospitalPatientTemporary> inHospitalPatientTemporaries =
                        JSONArray.parseArray(json.getJSONArray("data").toJSONString(),
                                InHospitalPatientTemporary.class);
                for (InHospitalPatientTemporary inHospitalPatientTemporary : inHospitalPatientTemporaries) {
                    InHospitalPatientResponse inHospitalPatientResponse = new InHospitalPatientResponse();
                    String patientSex = inHospitalPatientTemporary.getPatientSex();
                    if (StringUtils.isNotEmpty(patientSex)) {
                        switch (patientSex) {
                            case "男":
                                inHospitalPatientTemporary.setPatientSex("1");
                                break;
                            case "女":
                                inHospitalPatientTemporary.setPatientSex("2");
                                break;
                            default:
                                inHospitalPatientTemporary.setPatientSex("9");
                        }
                    }
                    BeanUtils.copyProperties(inHospitalPatientTemporary, inHospitalPatientResponse);
                    inHospitalPatientResponse.setPatientCode(inHospitalPatientTemporary.getPatientId());
                    inHospitalPatientResponseList.add(inHospitalPatientResponse);
                }
                baseResponse.setData(inHospitalPatientResponseList);
            } else {
                baseResponse.setCode(Constants.SUCCESS_CODE);
                baseResponse.setMessage(json.getString("message"));
            }
        } else {
            baseResponse.setCode(Constants.ERR_CODE);
            if (json != null) {
                baseResponse.setMessage(json.getString("message"));
            }
        }
        log.info("=listPatient-出参=>>>" + baseResponse.toString());
        return baseResponse;
    }*/


    /**
     * 获取住院病人列表-最新
     *
     * @param request request
     * @return list
     */
    @Override
    public BaseResponse<InHospitalPatientResponse> queryInHospitalPatientList(InHospitalpatientListQueryRequest request) {
        log.info("InHospitalPatientServiceImpl.queryInHospitalPatientList 入参：{}", JSONObject.toJSON(request));
        BaseResponse<InHospitalPatientResponse> result = new BaseResponse<InHospitalPatientResponse>();
        JSONObject jsonParams = JSONObject.parseObject(JSONObject.toJSONString(request));
        String testUrl = "http://interfacedemo.zk.atag.bsoft.com.cn/inHospitalPatient/listpatient";
        //JSONObject jsonObject = HttpClient.doPostJSONRequest(Constants.HIS_CLIENT_URL_01 + Constants.OUTPATIENT_LIST_PATIENT, jsonParams);
        JSONObject jsonObject = HttpClient.doPostJSONRequest(testUrl, jsonParams);
        log.info("InHospitalPatientServiceImpl.queryInHospitalPatientList 第三方返回数据:{}", jsonObject.toJSONString(jsonObject));
        try {
            if (null != jsonObject && jsonObject.getInteger("code") == ResultCode.SUCCESS) {
                InHospitalPatientResponse response = new InHospitalPatientResponse();
                if (jsonObject.containsKey("data") && jsonObject.get("data") != null) {
                    response = JSONObject.parseObject(jsonObject.get("data").toString(), InHospitalPatientResponse.class);
                }
                result.setData(response);
                log.info("InHospitalPatientServiceImpl.queryInHospitalPatientList 出参：{}", jsonObject.toJSONString(result));
            } else {
                result.setCode(jsonObject.getInteger("code"));
                result.setMessage(jsonObject.getString("message"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("HIS返回数据JSON解析异常" + e.getMessage());
            result.setCode(ResultCode.ERROR);
        }
        return result;
    }
}
